package com.exmaple.baidumv.fragment;

import java.util.ArrayList;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.example.baidumv.R;
import com.exmaple.baidumv.adapter.HomeAdapter;
import com.exmaple.baidumv.comm.App;
import com.exmaple.baidumv.entity.HomeBannerEntity;
import com.exmaple.baidumv.thread.BaseHttpThread;
import com.exmaple.baidumv.thread.RankThread;
import com.exmaple.baidumv.view.SlideShowView;
import com.yuelan.codelib.utils.JsonUtil;
import com.yuelan.codelib.utils.LOG;

public class HomeFragment extends Fragment{

	
	private View mRootView;
	private GridView mGridView;
	private HomeAdapter mAdapter;
	private ArrayList<HomeBannerEntity> array;
	
	private SlideShowView mSlideShowView ;
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mRootView = inflater.inflate(R.layout.fragment_home , null , false);
		Map<String, String> mpas = App.getUserBaseDeviceInfo(getActivity());
		mpas.put("type", "1");
		new Thread(new RankThread(getActivity(), mHandler, mpas)).start();
		init();
		initSlideShow();
		return mRootView;
	}
	private Handler mHandler =new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case BaseHttpThread.PostSuccess:
				JSONObject json;
				String o = (String)msg.obj;
				try {
					json = new JSONObject((String)msg.obj);
					int code = json.optInt("resultCode", 0);
					if(code == 200){
						JSONObject listString = new JSONObject("resultInfo");
						JSONArray jsonArray = listString.getJSONArray("list");  
						array = JsonUtil.getHomeJson(jsonArray);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mAdapter.setList(array);
				LOG.v(o);
				break;
			default:
				break;
			}
		};
	};

	private void init() {
		// TODO Auto-generated method stub
		mGridView = (GridView)mRootView.findViewById(R.id.grid_view);
		mAdapter = new HomeAdapter(getActivity());
		mGridView.setAdapter(mAdapter);
	}
	
	private void initSlideShow(){
		mSlideShowView = new SlideShowView(getActivity());
		
	}


}
